import os

AddOption(
    '--test',
    dest='build_test_code',
    action='store_true',
    help='build test code',
)

AddOption(
    '--gc',
    dest='clean_build',
    action='store_true',
    help='clean file',
)

AddOption(
    '--cli',
    dest='build_cli_client',
    action='store_true',
    help='build cli client',
)


def clean_build_file(dir, fix_list, exclude_dir):
    for root, dirs, files in os.walk(dir):
        flag = 0
        for each in exclude_dir:
            if os.path.join(TOP_DIR, each) == root:
                flag = 1
                break
        if flag == 1:
            continue
        for each in files:
            backfix = each.split('.')
            if len(backfix) <= 1:
                continue
            if backfix[-1] in fix_list:
                print("remove file:", os.path.join(root, each))
                os.remove(os.path.join(root, each))

def build_cli_client():
    cli_source_file = ['./src/cli/src/client.c']
    cli_include_file = ['./src/plat/include', './src/cli/include', './src/net/include']
    cli_depend_file = ['./build/obj/sys.o', './build/obj/server.o', './build/obj/cmd.o', './build/obj/cli/client.o']
    Depends("./build/obj/cli/client.o", TOP_DIR + '/build/mini-TCP-IP.exe')
    env.Object(target="./build/obj/cli/client.o", source = cli_source_file, CPPPATH = cli_include_file)
    env.Program(target=TOP_DIR + '/build/client.exe', source = cli_depend_file)


TOP_DIR = os.getcwd()
print(TOP_DIR)

env = Environment(ENV = {'PATH' : os.environ['PATH'], 'TEMP': os.environ['TEMP']})
# env = Environment(ENV = {'TEMP': os.environ['TEMP']})

env['CC'] = 'x86_64-w64-mingw32-gcc'
env['CCFLAGS'] = '-O2 -std=c11'
# env.AddPostAction("objdump -S ./build/mini-TCP-IP.exe")
# env['CXXFLAGS'] = '-std=c99'
# Export('env TOP_DIR')
if GetOption("clean_build"):
    clean_build_file(TOP_DIR, ['o', 'O', 'exe', 'txt'], [])
    print("clean build files success.")
    exit()

if GetOption("build_test_code"):
    SConscript('./test/SConscript', exports = "env TOP_DIR")

if GetOption("build_cli_client"):
    build_cli_client()

# APP_INCLUDE = ['./src/net/include/', './src/plat/', './test/', './third_party/npcap/Include/']
APP_INCLUDE = ['./src/net/include/', './src/plat/include/', './src/cli/include/']

# 指定源文件所在的文件夹
app_source_dir = ['./src/net/src/', './src/plat/src/']  # 假设源文件在名为 'src' 的子文件夹中
app_source_files = []
# 使用 Glob 函数获取所有源文件
for each in app_source_dir:
    app_source_files.extend(env.Glob(os.path.join(each, '*.c')))
app_source_files.extend(env.Glob("./src/cli/src/server.c"))
app_source_files.extend(env.Glob("./src/cli/src/cmd.c"))

for each in app_source_files:
    env.Object(target="./build/obj/{}.o".format(os.path.basename(str(each)).split('.')[0]), source = each,CPPPATH = APP_INCLUDE)

# 编译程序
app = env.Program(target=TOP_DIR + '/build/mini-TCP-IP.exe', source = env.Glob(os.path.join('./build/obj/', '*.o')), CPPPATH = APP_INCLUDE)
# 默认目标是构建程序
# output_file = str(os.path.join(TOP_DIR, '/build/mini-TCP-IP.exe'))
# env.InstallAs(TOP_DIR + '/build/mini-TCP-IP.exe', app)
# AddPostAction(app, "objdump -S ./build/mini-TCP-IP.exe > ./build/mini-net.lst")

# client_include_ssource = ['./src/cli/include/server.h', './src/net/include/stdtypes.h', './src/plat/include/sys.h']
# client_file_source = ['./src/cli/src/client.c']

# env.Object(target="./build/obj/client.o", source = ["./src/cli/src/client.c"], CPPPATH = APP_INCLUDE)
# env.Program(target=TOP_DIR + '/build/client.exe', source = ["./build/obj/client.o", "./build/obj/sys.o"])

Decider('content')
# scons -c -Q --debug=time